Quit Application Event
Quit Application Event
To handle the Quit Application event, your application should take any
actions that are necessary before it is terminated (such as saving any open
documents). The following program shows an example of a handler for the Quit
Application event.
When appropriate, the Finder sends your application a Quit Application
event immediately after a Print Documents event. The Finder also sends your
application a Quit Application event if the user chooses Restart or Shut
Down from the Finder's Special menu.
// A handler for the Quit Application event
// Assuming inclusion of
#include <AppleEvents.h>
pascal OSErr MyHandleQuit (AppleEvent * theAppleEvent, AppleEvent * reply,
long handlerRefcon);
OSErr MyGotRequiredParams (AppleEvent * theAppleEvent);
Boolean MyPrepareToTerminate (void);
pascal OSErr MyHandleQuit (AppleEvent * theAppleEvent, AppleEvent * reply,
long handlerRefcon)
{
Boolean userCanceled;
OSErr myErr;
// check for missing required parameters
myErr = MyGotRequiredParams( theAppleEvent);
if ( myErr) {
// an error occurred: do the necessary error handling
return myErr;
}
userCanceled = MyPrepareToTerminate();
if (userCanceled)
return userCanceledErr;
else
return noErr;
}
The handler in this program calls another function supplied by the
application, the MyPrepareToTerminate function. This function saves the
documents for any open windows and returns a Boolean value that indicates
whether the Quit request was canceled by the user. This is another example of
isolating code for interacting with the user from the code that performs the
requested action. Structuring your application in this way allows your
application to use the same routine when responding to a user event (such as
choosing the Quit command from the File menu) or to the cor responding
Apple event.
Note that your handler must not call the ExitToShell procedure. In the
program above, the application calls the ExitToShell procedure only if the
handler returns noErr as its function result.
A listing of MyGotRequiredParams may be found under
Writing Apple Event Handlers.